using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using static UnityEngine.EventSystems.EventTrigger;



public class ThenItem : MonoBehaviour
{
    public Dropdown m_WhatTypeToDo;
    public GameObject m_WhatDetailToDoParent;

    public GameObject m_DetailParameter;
    public PartStateBehaviourParameter m_PartStatePrefab;
    public ConditionList m_ConditionList;
    ShipEventBehaviours m_Behaviour;

    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    public void Init(ShipEventBehaviours behaviours)
    {
        if (behaviours == null)
            return;
        m_Behaviour = behaviours;
        m_WhatTypeToDo.value = (int)behaviours.m_BehavioursType;
    }
    public void OnWhatTypeToDoChanged(int value)
    {
        if (m_DetailParameter)
            Destroy(m_DetailParameter);
        if(m_Behaviour != null)
            m_Behaviour.m_BehavioursType = (ShipEventBehaviours.BehavioursType)value;
        switch (value)
        {
            case 0:
                break;
            case 1:
                var partStateUI = Instantiate(m_PartStatePrefab);
                partStateUI.m_ConditionList = m_ConditionList;
                partStateUI.gameObject.transform.SetParent(m_WhatDetailToDoParent.transform);
                partStateUI.onBehaviourChanged = OnBehaviourChanged;
                partStateUI.Init(m_Behaviour);
                LayoutRebuilder.ForceRebuildLayoutImmediate(transform.parent.gameObject.GetComponent<RectTransform>());
                LayoutRebuilder.ForceRebuildLayoutImmediate(gameObject.GetComponent<RectTransform>());
                m_DetailParameter = partStateUI.gameObject;
                break;
            case 2:
                break;
        }
    }
    public ShipEventBehaviours GetShipBehaviour()
    {
        //if (m_DetailParameter == null || !m_DetailParameter.GetComponent<PartStateBehaviourParameter>())
        //    return null;
        //var parameter = m_DetailParameter.GetComponent<PartStateBehaviourParameter>();
        return m_Behaviour;
    }

    public void OnBehaviourChanged(ShipEventBehaviours behaviour)
    {
        m_Behaviour = behaviour;
    }

}
